<?php

namespace App\Models\traits;

use Auth;
use App\Models\User;
trait Favour
{
    public function favours()
    {
        return $this->morphToMany(User::class, 'favour', 'favour')->withTimestamps();
    }

    public function getIsFavourAttribute()
    {
        return $this->favours()
            ->wherePivot('user_id', Auth::id())
            ->exists();
    }

    public function favour()
    {
        $method = $this->isFavour ? 'detach' : 'attach';

        $this->favours()->$method(Auth::id());
        $this->favour_count = $this->Favours()->count();
        $this->save();
        return true;
    }
}
